package com.hzau.tourist_service_sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzau.tourist_service_sys.dao.ComplaintDao;
import com.hzau.tourist_service_sys.dao.UserDao;
import com.hzau.tourist_service_sys.pojo.Complaint;
import com.hzau.tourist_service_sys.pojo.User;
import com.hzau.tourist_service_sys.result.Result;
import com.hzau.tourist_service_sys.service.ComplaintService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
@Transactional
@Service
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    ComplaintDao complaintDao;
    @Autowired
    UserDao userDao;
    String abslutepath="C:\\Users\\user\\Desktop\\往期作业\\游客服务系统\\ourist_service_sys\\src\\main\\resources\\static\\";
    int pageSize=10;
    //申请投诉(插入一个投诉)

    @Override
    public Result complaintInsert(Complaint complaint,long userId) throws IOException {
        User user = userDao.selectById(userId);
        if (complaint.getCpId()==null)
        {complaint.setCpSt( LocalDate.now());
        complaint.setUserName(user.getUserName());
        complaintDao.insert(complaint);}
        else if(complaint.getCpId()!=null){
            complaintDao.updateById(complaint);
        }
        return Result.success();
    }
    //查询所有未审批投诉
    @Override
    public Result findAllComplaintByPage(Complaint complaint) throws FileNotFoundException {

        int pagenumber=complaint.getPagenum();
        Integer status=complaint.getSolveStatus();
        String userName=complaint.getUserName();IPage page=new Page(pagenumber,pageSize);
        LambdaQueryWrapper<Complaint> lqw=new LambdaQueryWrapper<>();
        if(status!=null)
        lqw.eq(Complaint::getSolveStatus,status);
        if(userName!=null){
            lqw.eq(Complaint::getUserName,userName);
        }
        lqw.eq(complaint.getCpId()!=null,Complaint::getCpId,complaint.getCpId());
        lqw.orderByDesc(Complaint::getCpId);
        complaintDao.selectPage(page, lqw);
        List<Complaint> records = page.getRecords();
        for (Complaint record : records) {
            String s=record.getCpSrc();
            if(s==null){continue;}
            String[] filenames=s.split(":");
            for (String filename : filenames) {
                if (filename.equals(""))continue;
                record.getFiles().add(filename);
            }
        }
        return Result.success(records);
    }
    //查询特定id的投诉
//    @Override
    public Result findComplaintById(long contentId) {
        LambdaQueryWrapper<Complaint> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Complaint::getCpId,contentId);
        Complaint complaint = complaintDao.selectOne(lqw);

        return Result.success(complaint);
    }

    @Override
    public Result updateStatusById(long contentid,Integer status) {
        Complaint complaint=new Complaint();
        complaint.setCpId(contentid);
        complaint.setSolveStatus(status);
        complaintDao.updateById(complaint);
        return Result.success();
    }


    @Override
    public Result replyUpdte(Complaint complaint) {
        complaintDao.updateById(complaint);

        return Result.success();
    }

    @Override
    public Result fileUpload(MultipartFile multipartFile) throws IOException {
        String names = "";
//        for (MultipartFile multipartFile : multipartFiles)
            if(multipartFile!=null){
            String name=multipartFile.getOriginalFilename();
            int off =name.lastIndexOf(".");
            String type=name.substring(off);
            UUID uuid=UUID.randomUUID();
            name=uuid+type;
            multipartFile.transferTo(new File(abslutepath+name));
            names=name;

        };
        return Result.success(names);
    }


}
